﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace Manage_Pupils
{
    public partial class FormThemHS : Form
    {
        private SqlConnection con;
        private DataTable dt = new DataTable("tblStudent");
        private DataTable dtClass = new DataTable("tblClass");
        private SqlDataAdapter da = new SqlDataAdapter();
        private Boolean kt;
        private void connect()
        {
            try
            {
                con = new SqlConnection("Server=IWANTWIN-PC;user id=sa;password=123456;database=QuanLyHocSinh");
                con.Open();

            }
            catch
            {
                MessageBox.Show("Ban da ket noi that bai", "Loi", MessageBoxButtons.OK);
            }
        }
        //Giai phong bo nho
        private void disconnect()
        {
            con.Close();
            con.Dispose();
            con = null;

        }
        private void getdata()
        {
            SqlCommand command = new SqlCommand();
            command.Connection = con;
            command.CommandType = CommandType.Text;
            command.CommandText = "Select * from HocSinh";
            da.SelectCommand = command;
            da.Fill(dt);

            //Tao combox cho gioi tinh
            command.CommandText = "Select * from HocSinh";
            da.SelectCommand = command;
            da.Fill(dt);
            comboGioiTinh.DataSource = dt;
            comboGioiTinh.DisplayMember = "GioiTinh";

            //Tao combox cho Ten lop

            command.CommandText = "Select * from Lop";
            da.SelectCommand = command;
            da.Fill(dtClass);
            comboBoxMaLop.DataSource = dtClass;
            comboBoxMaLop.DisplayMember = "TenLop";
            comboBoxMaLop.ValueMember = "MaLop";
            comboBoxMaLop.SelectedValue = "Malop";

        }
        public FormThemHS()
        {
            InitializeComponent();
        }

        private void buttonThoat_Click(object sender, EventArgs e)
        {
            this.Close();
            FormCapNhatHS fr = new FormCapNhatHS();
            fr.Show();
        }

        private void FormThemHS_Load(object sender, EventArgs e)
        {
            connect();
            getdata();

        }
        private bool kiemtra()
        {
            kt = true;
            if ((textMaHS.Text == "") ||(textTenHS.Text ==""))
            {
                MessageBox.Show("Ban chua nhap ma hoac ten  cua SV!", "Thong bao", MessageBoxButtons.OK);
                kt = false;

            }
            else
                if ((textMaHS.Text == null)||(textTenHS.Text ==null))
                {
                    MessageBox.Show("Ban chua nhap ma hoac ten cua SV!", "Thong bao", MessageBoxButtons.OK);
                    kt = false;

                }
            return kt;
        }

        private void buttonLuu_Click(object sender, EventArgs e)
        {
            kiemtra();
            if (kt == true)
            {
                DataRow row = dt.NewRow();  //Tao mot doi tuong de quan ly mot hang cua mot DataTable, phai kem theo doi tuong DataTable
                row["MaHS"] = textMaHS.Text;
                row["HoHS"] = textHoHS.Text;
                row["TenHS"] = textTenHS.Text;
                row["NgaySinh"] = textNgaySinh.Text;
                if (comboGioiTinh.Text == "Name")
                    row["GioiTinh"] = 1;
                else if (comboGioiTinh.Text == "Nu")
                    row["GioiTinh"] = 0;
                row["DiaChi"] = textDiaChi.Text;
                row["DienThoai"] = textDienThoai.Text;
                row["MaLop"] = comboBoxMaLop.SelectedValue;

                dt.Rows.Add(row);
                SqlCommand commandInsert = new SqlCommand();
                commandInsert.Connection = con;
                commandInsert.CommandType = CommandType.Text;
                commandInsert.CommandText = @"Insert HocSinh (MaHS, HoHS, TenHS, NgaySinh, GioiTinh, DiaChi, DienThoai, MaLop)
                                                        Values (@MaHS, @HoHS, @TenHS, @NgaySinh, @GioiTinh, @DiaChi, @DienThoai, @MaLop)";

                commandInsert.Parameters.Add("@MaHS", SqlDbType.NChar, 10, "MaHS");
                commandInsert.Parameters.Add("@HoHS", SqlDbType.NChar, 10, "HoHS");
                commandInsert.Parameters.Add("@TenHS", SqlDbType.NChar, 10, "TenHS");
                commandInsert.Parameters.Add("@NgaySinh", SqlDbType.Date, 50, "NgaySinh");
                commandInsert.Parameters.Add("@GioiTinh", SqlDbType.Int, 10, "GioiTinh");
                commandInsert.Parameters.Add("@DiaChi", SqlDbType.NChar, 10, "DiaChi");
                commandInsert.Parameters.Add("@DienThoai", SqlDbType.NChar, 10, "DienThoai");
                commandInsert.Parameters.Add("@MaLop", SqlDbType.NChar, 10, "MaLop");
                da.InsertCommand = commandInsert;
                da.Update(dt);
                MessageBox.Show("Ban da them thanh cong !", "Thong bao", MessageBoxButtons.OK);
                FormCapNhatHS fr = new FormCapNhatHS();
                fr.Show();
                Close();
            }
        }
    }
}
